import cv2 as cv
import numpy as np


def main():
    filePath = '../data/circles.png'
    img = cv.imread(filePath)
    imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    _, thresh = cv.threshold(imgGray, 127, 255, cv.THRESH_BINARY)
    contours, _ = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)

    SymbolArea = 11560
    offset = round(SymbolArea * 0.2)
    validIdx = [i for i, c in enumerate(contours) if SymbolArea - offset < cv.contourArea(c) < SymbolArea + offset]

    imgBackGround = np.zeros_like(img)
    for i in validIdx:
        cv.drawContours(imgBackGround, contours, i, (0, 0, 255), 1)

    cv.imshow("img", img)
    cv.imshow("imgBG", imgBackGround)
    cv.waitKey(0)
    cv.destroyAllWindows()


if __name__ == "__main__":
    main()
